﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.EntityFramework.ModelMapper;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.EntityFramework.Implementation.Dictionary
{
    /// <summary>
    /// CustomerDao  class
    /// </summary>
    public class EntityUnitDao : IUnitDao
    {

        public List<BusinessObjects.BussinessDictionary.Unit> GetUnits(int isActive)
        {
            throw new NotImplementedException();
        }

        public List<BusinessObjects.BussinessDictionary.Unit> GetUnits()
        {
            throw new NotImplementedException();
        }

        public IList<BusinessObjects.BussinessDictionary.Unit> GetUnitsByParentId(int parentId)
        {
            throw new NotImplementedException();
        }

        public long GetSequence()
        {
            throw new NotImplementedException();
        }

        public List<BusinessObjects.BussinessDictionary.Unit> GetPaged(int unitId, int currentPage)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var pageSize = 0;
                var result = context.Units.AsQueryable().Where(x => x.UnitID == unitId).OrderByDescending(x => x.UnitID).ToList();
                var option = context.DBOptions.SingleOrDefault(x => x.Code == "NumberOfPage");
                if (option != null)
                {
                    int.TryParse(option.Value, out pageSize);
                }
                if (pageSize > 0 && currentPage > 0)
                {
                    var start = (currentPage - 1) * pageSize;
                    result = result.Skip(start).Take(pageSize).ToList();
                }
                return result.Map().ToList();
            }
        }

        public int GetPageSize()
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var pageSize = 0;
                var option = context.DBOptions.SingleOrDefault(x => x.Code == "NumberOfPage");
                var totalRow = context.Units.Count();
                if (option != null)
                {
                    int.TryParse(option.Value, out pageSize);
                }
                if (pageSize > 0)
                {
                    var round = totalRow % pageSize;
                    if (round > 0)
                    {
                        return totalRow / pageSize + 1;
                    }
                    return totalRow / pageSize;

                }
                return 1;
            }
        }

        public BusinessObjects.BussinessDictionary.Unit GetUnit(int unitId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                return context.Units.SingleOrDefault(x => x.UnitID == unitId).Map();
            }
        }

        public BusinessObjects.BussinessDictionary.Unit GetUnitByUserID(int userId)
        {
            throw new NotImplementedException();
        }

        public string InsertUnit(BusinessObjects.BussinessDictionary.Unit unit)
        {
            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    context.Units.Add(unit.Map());
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            return null;
        }

        public string UpdateUnit(BusinessObjects.BussinessDictionary.Unit unit)
        {
            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    var result = context.Units.SingleOrDefault(x => x.UnitID == unit.UnitId);
                    if (result != null)
                    {
                        result.Name = unit.Name;
                        result.UnitID = unit.UnitId;
                        result.Code = unit.Code;
                        result.ParentID = unit.ParentId;
                        result.HasParent = unit.HasParent;
                        result.IsActive = unit.IsActive;

                        result.PhoneNumber = unit.PhoneNumber;
                        result.Address = unit.Address;
                        result.ByManagement = unit.ByManagement;
                        result.Grade = unit.Grade;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            return null;
        }

        public string DeleteUnit(BusinessObjects.BussinessDictionary.Unit unit)
        {
            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    var result = context.Units.SingleOrDefault(x => x.UnitID == unit.UnitId);
                    if (result != null)
                    {
                        context.Units.Remove(result);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            return null;
        }
    }
}
